import {
	setToken,
	removeToken
} from '@/utils/auth'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import store from "@/store/index.js"
import constant from '@/utils/constant'
import avatar from "@/static/images/profile.jpg"
const state = () => ({
	id: uni.getStorageSync(constant.id) || "",
	name: uni.getStorageSync(constant.name) || "",
	avatar: uni.getStorageSync(constant.avatar) || "",
	roles: uni.getStorageSync(constant.roles) || [],
	permissions: uni.getStorageSync(constant.permissions) || [],
	token: ''
})
const getters = {

}
const mutations = {
	SETOBJ(state, obj) {
		for (let key in obj) {
			if (constant[key]) {
				console.log(key);
				uni.setStorageSync(constant[key], obj[key]);
			}
			state[key] = obj[key]
		}
	},

}
const actions = {
	LogOut({
		commit
	}) {
		return new Promise((resolve, reject) => {
			logout().then(() => {
				commit("SETOBJ", {
					roles: [],
					permissions: [],
				})
				removeToken()
				uni.clearStorageSync();
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	},

	inserted({ state }, permissionFlag) {
		const all_permission = "*:*:*"; // 全部权限
		const permissions = state.permissions || [] // 用户拥有的权限标识的数组
		// console.log(permissions);
		const hasPermissions = permissions.some(permission => {
			return all_permission === permission || permissionFlag.includes(permission)
		})
		return hasPermissions ? true : false
	},
	// 登录
	Login({
		dispatch
	}, userInfo) {
		console.log(userInfo, "userInfo");
		const username = userInfo.username.trim()
		const password = userInfo.password
		const captchaVerification = userInfo.captchaVerification

		return new Promise((resolve, reject) => {
			login(username, password, captchaVerification).then(async res => {
				console.log(res);
				res = res.data;
				// 设置 token
				setToken(res)
				await dispatch("getUserInfo");
				resolve()
			}).catch(error => {
				console.log(error);
				reject(error)
			})
		})
	},
	// 获取用户信息
	async getUserInfo({
		commit
	}) {
		await store.dispatch("dict/loadDictDatas")
		return new Promise(async (resolve, reject) => {
			getInfo().then(res => {
				res = res.data; // 读取 data 数据
				const user = res.user
				const avatar = (user == null || user.avatar === "" || user.avatar == null) ?
					avatar : user.avatar
				const nickname = (user == null || user.nickname === "" || user
					.nickname == null) ? "" : user.nickname
				const id = (user == null || user.id === "" || user.id == null) ? "" :
					user.id
				let setobj = {}
				if (res.roles && res.roles.length > 0) {
					setobj.roles = res.roles;
					setobj.permissions = res.permissions;
				} else {
					setobj.roles = ['ROLE_DEFAULT'];
				}
				setobj.avatar = avatar;
				setobj.name = nickname;
				setobj.id = id;
				setobj.token = res.token;
				commit("SETOBJ", setobj)
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},

}
export default {
	state,
	getters,
	mutations,
	actions
}